/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beantown;


import econosim.*;
import simframe.util.*;
import simframe.simulation.*;

/**
 *
 * @author Steve Noble
 */

public class Consumer extends UtilityAgent {

  static final double healthMax = 30;
  static final double rda = 10;

    // Actions that consumers perform
    public static final Swarm.SwarmAction<Consumer> shop = new Swarm.SwarmAction<Consumer>() {
        @Override
        public void exec(Consumer con) {
            con.shop();
        }
    };

    public static final Swarm.SwarmAction<Consumer> updateHealth = new Swarm.SwarmAction<Consumer>() {
        @Override
        public void exec(Consumer con) {
            con.updateHealth();
        }
    };

    public static final Swarm.SwarmAction<Consumer> getDole = new Swarm.SwarmAction<Consumer>() {
        @Override
        public void exec(Consumer con) {
            con.getDole();
        }
    };


  double dole;      // home much outside money is received each day

  private final Market market;
  private final Location loc;
  // dimensions of my world
  Consumer(Market market, Location loc)
  {
      this.loc = loc;
      this.market = market;

	  dole = 15;

	  reset();
  }

  // reset me
    @Override
    public void reset() {

        setSavings(1000);

        this.setUtility(healthMax);
    }


    public boolean dead() {
        return this.getUtility() < 0;
    }

  // change my parameter
  public double updateDole(double value)
  {
      
     dole = 10 + 30*value;
     
     return dole;
  }

  // simulation actions
  private void shop()
  {
      if (dead()) {
          return;
      }
      
	  double uNeed = healthMax - this.getUtility();

      if (uNeed > 0 && this.getSavings() > 0 ) {

          Offer offer = new Offer(uNeed, this.getSavings(), Offer.Type.Buy, this, 1);

          market.addOffer(offer);
      }
  }

  public void updateHealth() {

      if (dead()) {
          return;
      }
		  this.reduceUtility(rda);
    }
  
   private void getDole() {
         
		  this.receiveMoney(dole);
    }

    public Location getLoc() {
       return loc;
    }
  
}
